<!DOCTYPE html>
<html>

	<head lang="en">
		<title>骡窝日报</title>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
		<script src="/js/bootstrap/js/bootstrap.min.js"></script>
		<link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
		<script src="/js/jquery/jquery.min.js"></script>
		<link rel="stylesheet" href="/css/font/css/font-awesome.min.css" />
		<link rel="stylesheet" href="/css/reset.css" />
		<link rel="stylesheet" href="/css/news.css" />
		<script src="/js/plugins/jrender/jrender.min.js"></script>
		<script src="/js/common.js"></script>
		<link rel="stylesheet" href="../js/plugins/dialog2/dialog.css">
		<script src="../js/plugins/dialog2/dialog.min.js"></script>
	
	</head>

	<script>
		$(function () {
		    //拿到登录的用户信息,用于收藏和回显
            var user=JSON.parse(sessionStorage.getItem("user"));
            var userId=user.id;


		    //搜索功能
            var keyword="";
            var pages = 1;
            var currentPage = 1;
            var temp = [];
			$(".searchBtn").click(function () {
				keyword=$(this).val();
			    if (keyword!=null && keyword.length>0){
                    pages = 1;
                    currentPage = 1;
                    temp = [];
                	query();
                    scroll();
				}
            });


            function query() {
                $.get("/dailies",{pageSize:5,currentPage:currentPage,keyword:keyword,orderBy:"releaseTime desc"},function (data) {
                    $.merge(temp,data.list);
                    $("#daily").renderValues({list:temp},{
                        setHref:function (item,value) {
                            var href = $(item).attr("href")+value;
                            $(item).attr("href",href);
                        },
						setContent:function (item, value) {
                            console.log(value);
                            $(item).attr("content",value);
                        }
                    });

                    //回显收藏
                    $.get("/dailies/selectCollectionByUserId",{userId:userId},function (data) {
                        $.each(data,function (index,ele) {
							$("i[content='"+ele+"']").css("color","red")
                        })
                    });

                    currentPage += 1;
                    pages = data.pages;
                });
            }



            function scroll() {
                $(window).scroll(function () {
                    if ($(window).height() + $(window).scrollTop()+1 >= $(document).height()) {
                        if (currentPage <= pages) {
                            query();
                        } else {
                            $(document).dialog({
                                type : 'notice',
                                infoText: '我是个有底线的人!',
                                autoClose: 2500,
                                position: 'bottom'  // center: 居中; bottom: 底部
                            });
                        }
                    }
                })
            };
            query();
            scroll();

            //收藏功能
			$("#daily").on("click",".fa-heart",function () {
                var dailyId=$(this).attr("content");
                $.get("/dailies/saveOrDeleteCollection",{dailyId:dailyId,userId:userId},function (data) {
                    if ((data.msg)=="收藏成功"){
                        $("i[content='"+dailyId+"']").css("color","red")
                    }else {
                        $("i[content='"+dailyId+"']").css("color","black")
                    }
                })
            });
        })
	</script>
	<body>
		

		<div class="search-head">
			<div class="row nav-search">
				<div class="col-2">
					<a href="../index.html">
						<span><i class="fa fa-chevron-left fa-2x"></i></span>
					</a>
				</div>
				<div class="col-10">
					<div class="input-group-sm search">
							<input class="form-control searchBtn" placeholder="搜索资讯">
					</div>
				</div>
			</div>
		</div>

		<div class="container news">
			<div class="news">
				<h6 class="news-title">骡窝日报 </h6>
				<div class="row" id="daily">
					<div  render-loop="list">
						<div class="col-7">
							<a href="/mine/newsContent.html?id=" render-fun="setHref" render-key="list.id">
								<img class="rounded" width="170%" height="150px"  render-src="list.coverUrl">
							</a>
								<div class="caption">
									<p>
										<span > </span>
										<i class="fa fa-heart" content="" render-fun="setContent" render-key="list.id">
										</i>
									</p>
									<p render-html="list.title"></p>
								</div>
						</div>
					</div>
				</div>
			</div>
		</div>



</body>
</html>